package com.online_vote_system.demo.controller;

import com.online_vote_system.demo.service.AdminService;
import com.online_vote_system.demo.service.CompetitionService;
import com.online_vote_system.demo.vo.Admin;
import com.online_vote_system.demo.vo.Competition;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@Slf4j
@Controller
public class AdminController {

    @Autowired
    AdminService adminService;
    @Autowired
    CompetitionService competitionService;

    @GetMapping(value = {"/Admin"})
    public String Audience(){
        return "html/AdminLogin";
    }

    @PostMapping("/LoginTest")
    public String LoginTest(String adminName,
                            String adminPass,
                            Model model,
                            HttpServletRequest request){
        Admin admin = adminService.LoginTest(adminName,adminPass);
        HttpSession session = request.getSession();
        if(admin == null){
            model.addAttribute("msg","账号或密码错误");
            return "html/AdminLogin";
        }else {
            log.info(admin.toString());
            session.setAttribute("loginUser", admin);
            if (admin.getAdminType().equals("0")) {//root管理员
                return "redirect:/AdminOs";
            } else {//比赛管理员
                return "redirect:/CompetitionOs";
            }
        }
    }
    @GetMapping("/LoginOut")
    public String LoginOut(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.setAttribute("loginUser",null);
        return "html/AudienceLogin";
    }
    @GetMapping("/showScore")
    public String showScore(Model model){
        Competition competition = competitionService.getOneByType();
        if(competition == null){
            return "html/Admin/noCompetition";
        }
        model.addAttribute("competition",competition);
        return "html/Admin/showScore";
    }
}
